'use strict'

import axios from 'axios'
import store from '@/store'
import router from '@/router'
import Tost from '@/plugins/toast'

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  baseURL: process.env.baseURL || process.env.apiUrl || '',
  // timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
  // baseURL: 'http://localhost:8000',
  timeout: 3600 // Timeout
}

const _axios = axios.create(config)

_axios.interceptors.request.use(
  function (config) {
    if (store.getters.token) {
      config.headers['Authorization'] = 'Bearer ' + store.getters.token
    }
    // Do something before request is sent
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    // Do something with response data
    const token = response.headers.authorization
    if (token) {
      // 如果 header 中存在 token，那么触发 refreshToken 方法，替换本地的 token
      store.commit('setToken', token.substring(7))
    }
    return response.data
  },
  function (error) {
    if (error && error.response) {
      const token = error.response.headers.authorization
      if (token) {
        // 如果 header 中存在 token，那么触发 refreshToken 方法，替换本地的 token
        store.commit('setToken', token.substring(7))
      }
      switch (error.response.status) {
        case 400:
          error.message = '请求错误'
          break

        case 401:
          error.message = '未登录授权'
          // 退出登录 todo
          store.commit('clearAll')
          router.push({
            name: 'login'
          })
          break

        case 403:
          error.message = '拒绝访问'
          router.push({
            name: '403'
          })
          break

        case 404:
          error.message = `请求地址出错: ${error.response.config.url}`
          break

        case 408:
          error.message = '请求超时'
          break

        case 422:
          error.message = error.response.data.errors[0]['code']
          break

        case 429:
          error.message = error.response.data.message
          break
        case 500:
          error.message = '服务器内部错误'
          break

        case 501:
          error.message = '服务未实现'
          break

        case 502:
          error.message = '网关错误'
          break

        case 503:
          error.message = '服务不可用'
          break

        case 504:
          error.message = '网关超时'
          break

        case 505:
          error.message = 'HTTP版本不受支持'
          break

        default:
      }
    }
    Tost.error({
      msg: error.message
    })
    // Do something with response error

    return Promise.reject(error)
  }
)

export default _axios
